//
//  mm_multipleCustomerToAllocateViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-7-1.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_multipleCustomerToAllocateViewController.h"
#import "mm_selectCustomersViewController.h"
#import "mm_tableView1Cell.h"
#import "mm_tableView2Cell.h"

@interface mm_multipleCustomerToAllocateViewController ()

//通过陈述号和作业指示号统计出金额，金额的表现形式为一字符串
-(CGFloat)returnSumMoneyByStateNumber:(NSString *)stateNumber andIndicationNumber:(NSString *)indicationNumber;
-(CGFloat)returnMoneySumFromArrayByKeyStr:(NSString *)keyStr withArray:(NSArray *)array;

//根据分配状态ID取Name
-(NSString *)getAllocationTypeNameById:(NSString *)allocationTypeId;

//刷新分配列表
-(void)updateAllocationList;
//刷新客户列表
-(void)updateCustomerList;
//指定分配列表行的总金额
-(CGFloat)totalMoneyByAllocationList:(NSInteger)allocationRowSelXingCan;
//指定分配列表行中的除过指定客户列表行的其它总金额
-(CGFloat)otherTotalMoneyByAllocationList:(NSInteger)allocationRowSelXingCan andCustomerList:(NSInteger)customerListRowSelXingCan;

@end

@implementation mm_multipleCustomerToAllocateViewController

@synthesize tableViewDis_1 = _tableViewDis_1;
@synthesize tableViewDis_2 = _tableViewDis_2;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _arrAllKeysOfAllocationList = [NSArray arrayWithObjects:
                                   MM_ALLOCATION_STATEMENT_NUMBER_KEY_STR_,
                                   MM_ALLOCATION_INDICATION_NUMBER_KEY_STR_,
                                   MM_ALLOCATION_INDICATION_CONTENT_KEY_STR_,
                                   MM_ALLOCATION_SERVICE_TYPE_KEY_STR_,
                                   MM_ALLOCATION_REPAIR_MONEY_KEY_STR_,
                                   MM_ALLOCATION_ALLOCATION_STATUS_KEY_STR_, nil];
    
    _dicAllLblsWhileAllocationListToBeSelected = [NSMutableDictionary dictionaryWithCapacity:[_arrAllKeysOfAllocationList count]];
    
    /*
     陈述号：100
     指示号：101
     作业指示：102
     服务类型：103
     维修金额：104
     分配状态：105
     */
    
    //将所有的label添加进 _dicAllLblsWhileAllocationListToBeSelected 中
    
    for (int i = 0; i<[_arrAllKeysOfAllocationList count]; i++)
    {
        [_dicAllLblsWhileAllocationListToBeSelected setObject:(UILabel *)[self.view viewWithTag:100+i] forKey:[_arrAllKeysOfAllocationList objectAtIndex:i]];
    }
    
    //初始化表单选中标识
    _nRowAllocationListToSel = 0;
    _nRowCustomerListToSel = 0;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //刷新界面数据
    [self.tableViewDis_1 reloadData];
    [self.tableViewDis_2 reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - button 的回调方法

- (IBAction)backBtnPressed:(UIButton *)sender
{
//    NSLog(@"backBtnPressed !!!");
    
    //当退出多客户结算界面时，将每一个客户列表的最后一名客户的 余额承担标志 设置为 承担余额
    [self setUpTheBalanceTagOfLastCustomer];
    
//    NSLog(@"%@",[MainMenuDataModel sharedMainMenuDataModel].arrAllocationList);
    
    //跳回上一级页面
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)homeBtnPressed:(UIButton *)sender
{
//    NSLog(@"homeBtnPressed !!!");
    
    //跳回主界面
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)selectCustomerBtnPressed:(UIButton *)sender
{
//    NSLog(@"selectCustomerBtnPressed !!!");
    
    //跳转到客户选择界面
    mm_selectCustomersViewController *vc = [[mm_selectCustomersViewController alloc] initWithNibName:@"mm_selectCustomersViewController" bundle:nil];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
//    NSLog(@"%i,%i",_nRowAllocationListToSel,_nRowCustomerListToSel);
    
    _strStateNumber = [[modelIns.arrAllocationList objectAtIndex:_nRowAllocationListToSel] objectForKey:MM_ALLOCATION_STATEMENT_NUMBER_KEY_STR_];
    _strIndicationNumber = [[modelIns.arrAllocationList objectAtIndex:_nRowAllocationListToSel] objectForKey:MM_ALLOCATION_INDICATION_NUMBER_KEY_STR_];
    
    vc.strStateNumber = _strStateNumber;
    vc.strIndicationNumber = _strIndicationNumber;
    
//    NSLog(@"%@,%@",vc.strStateNumber,vc.strIndicationNumber);
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)deleteCustomerBtnPressed:(UIButton *)sender
{
//    NSLog(@"deleteCustomerBtnPressed !!!");
}

- (IBAction)okBtnPressed:(UIButton *)sender
{
//    NSLog(@"okBtnPressed !!!");
}

- (IBAction)closeBtnPressed:(UIButton *)sender
{
//    NSLog(@"closeBtnPressed !!!");
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidUnload {
    [self setTableViewDis_1:nil];
    [self setTableViewDis_2:nil];
    [super viewDidUnload];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [self updateAllocationList];
    [self updateCustomerList];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrTmp = modelIns.arrAllocationList;
    if (tableView == self.tableViewDis_1)
    {
        ;
    }
    else
    {
        arrTmp = [[arrTmp objectAtIndex:_nRowAllocationListToSel] objectForKey:@"customerList"];
    }
    
    return 3+[arrTmp count];
}

//哪几行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
    
    if (tableView == self.tableViewDis_2)
    {
        NSInteger row = [indexPath row];
        MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
        NSArray *arrTmp = modelIns.arrAllocationList;
        arrTmp = [[arrTmp objectAtIndex:_nRowAllocationListToSel] objectForKey:@"customerList"];
        if (row<[arrTmp count])
        {
            return YES;
        }
        return NO;
    }
    
    return NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    mm_tableView1Cell*cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (tableView == self.tableViewDis_1)
    {
        if (nil == cell)
        {
            cell = [[mm_tableView1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    }
    else
    {
        if (nil == cell)
        {
            cell = [[mm_tableView2Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrTmp = modelIns.arrAllocationList;
    
    if (tableView == self.tableViewDis_1)
    {
        ;
    }
    else
    {
        arrTmp = [[arrTmp objectAtIndex:_nRowAllocationListToSel] objectForKey:@"customerList"];
    }
    
    if (row<[arrTmp count])
    {
        //设置cell为可选状态
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
        //设置cell数据
        NSDictionary *dicTmp = [arrTmp objectAtIndex:row];
        for (int i = 0; i<[cell.arrForKeys count]; i++)
        {
            NSString *strKey = [cell.arrForKeys objectAtIndex:i];
            
//            //维修金额
//            if ([strKey isEqualToString:MM_ALLOCATION_REPAIR_MONEY_KEY_STR_] && tableView == self.tableViewDis_1)
//            {
//                ((UILabel *)[cell.dicForLblsOnCell objectForKey:strKey]).text = [NSString stringWithFormat:@"%0.2f",[self returnSumMoneyByStateNumber:[dicTmp objectForKey:MM_ALLOCATION_STATEMENT_NUMBER_KEY_STR_] andIndicationNumber:[dicTmp objectForKey:MM_ALLOCATION_INDICATION_NUMBER_KEY_STR_]]];
//                continue;
//            }
            
            //服务类型
            if ([strKey isEqualToString:MM_ALLOCATION_SERVICE_TYPE_KEY_STR_] && tableView == self.tableViewDis_1)
            {
                ((UILabel *)[cell.dicForLblsOnCell objectForKey:strKey]).text = [HelpClass getServiceTypeNameById:[dicTmp objectForKey:MM_ALLOCATION_SERVICE_TYPE_KEY_STR_]];
                continue;
            }
            
            //分配状态
            if ([strKey isEqualToString:MM_ALLOCATION_ALLOCATION_STATUS_KEY_STR_] && tableView == self.tableViewDis_1)
            {
                
                ((UILabel *)[cell.dicForLblsOnCell objectForKey:strKey]).text = [self getAllocationTypeNameById:[dicTmp objectForKey:strKey]];
                continue;
            }
            
            ((UILabel *)[cell.dicForLblsOnCell objectForKey:strKey]).text = [dicTmp objectForKey:strKey];
        }
    }
    else
    {
        //设置cell为不可选状态
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //cell上的数据设置为全空
        for (int i = 0; i<[cell.arrForKeys count]; i++)
        {
            NSString *strKey = [cell.arrForKeys objectAtIndex:i];
            ((UILabel *)[cell.dicForLblsOnCell objectForKey:strKey]).text = @"";
        }
    }
    
    return cell;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableViewDis_2)
    {
        return @"编辑";
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrTmp = modelIns.arrAllocationList;
    
    //判断出tableView要显示的列表
    if (tableView == self.tableViewDis_1)
    {
        ;
    }
    else
    {
        arrTmp = [[arrTmp objectAtIndex:_nRowAllocationListToSel] objectForKey:MM_CUSTOMER_LIST_KEY_STR_];
    }
    
    //根据条件判断响应点击操作
    if (row<[arrTmp count])
    {
        //响应点击操作
        if (tableView == self.tableViewDis_1)
        {
            mm_tableView1Cell *cell = [tableView cellForRowAtIndexPath:indexPath];
            for (int i = 0; i<[cell.arrForKeys count]; i++)
            {
                NSString *strKey = [cell.arrForKeys objectAtIndex:i];
                ((UILabel *)[_dicAllLblsWhileAllocationListToBeSelected objectForKey:strKey]).text = ((UILabel *)[cell.dicForLblsOnCell objectForKey:strKey]).text;
            }
            
            //更新属性的值
            _nRowAllocationListToSel = row;
            
            _strStateNumber = ((UILabel *)[cell.dicForLblsOnCell objectForKey:MM_ALLOCATION_STATEMENT_NUMBER_KEY_STR_]).text;
            _strIndicationNumber = ((UILabel *)[cell.dicForLblsOnCell objectForKey:MM_ALLOCATION_INDICATION_NUMBER_KEY_STR_]).text;
            
            //刷新tableView2
            [self.tableViewDis_2 reloadData];
        }
        else
        {
            _nRowCustomerListToSel = row;
        }
    }
    else
    {
//        if (tableView == self.tableViewDis_1)
//        {
//            _nRowAllocationListToSel = -1;
//            _nRowCustomerListToSel = -1;
//            [self.tableViewDis_1 reloadData];
//            [self.tableViewDis_2 reloadData];
//        }
//        else
//        {
//            _nRowCustomerListToSel = -1;
//            [self.tableViewDis_2 reloadData];
//        }
        
        return;
    }
}

#pragma mark - 出现删除按钮时，点击按钮时的操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"编辑状态......");
    
    _editPopViewDis = nil;
    _editPopViewDis = [[mm_multipleCustomerToAllocateEditViewController alloc] initWithNibName:@"mm_multipleCustomerToAllocateEditViewController" bundle:nil];
    
    mm_tableView2Cell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //初始化 弹出界面的控件内容
    _editPopViewDis.customerIdLbl.text = ((UILabel *)[cell.dicForLblsOnCell objectForKey:MM_CUSTOMER_NUMBER_KEY_STR_]).text;
    _editPopViewDis.customerNameLbl.text = ((UILabel *)[cell.dicForLblsOnCell objectForKey:MM_CUSTOMER_NAME_KEY_STR_]).text;
    _editPopViewDis.repairMoneyTxt.text = ((UITextField *)[cell.dicForLblsOnCell objectForKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_]).text;
    
    _editPopViewDis.nRowAllocationListToSel = _nRowAllocationListToSel;
    _editPopViewDis.nRowCustomerListToSel = [indexPath row];
    
    [self presentPopupViewController:_editPopViewDis animationType:MJPopupViewAnimationFade];
}

//通过陈述号和作业指示号统计出金额
-(CGFloat)returnSumMoneyByStateNumber:(NSString *)stateNumber andIndicationNumber:(NSString *)indicationNumber
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    CGFloat fMoneySum = 0.0;
    
    for (int i = 0; i<[modelIns.arrForResults count]; i++)
    {
        NSString *stateNumberTmp = [[modelIns.arrForResults objectAtIndex:i] objectForKey:MM_RESULTS_STATE_NUMBER_KEY_STR_];
        NSArray *arrIndicationTmp = [[modelIns.arrForResults objectAtIndex:i] objectForKey:MM_INDICATION_LIST_KEY_STR_];
        for (int j = 0; j<[arrIndicationTmp count]; j++)
        {
            NSString *indicationNumberTmp = [[arrIndicationTmp objectAtIndex:j] objectForKey:MM_INDICATION_INDICATION_NUMBER_KEY_STR_];
            if ([stateNumber isEqualToString:stateNumberTmp]
                &&
                [indicationNumber isEqualToString:indicationNumberTmp])
            {
                CGFloat repairItemMoney = [self returnMoneySumFromArrayByKeyStr:MM_REPAIR_ITEM_REPAIR_WORK_CHARGE_KEY_STR_ withArray:[[arrIndicationTmp objectAtIndex:j] objectForKey:MM_REPAIR_ITEM_LIST_KEY_STR_]];
                CGFloat partMoney = [self returnMoneySumFromArrayByKeyStr:MM_PART_UNITY_PRICE_KEY_STR_ withArray:[[arrIndicationTmp objectAtIndex:j] objectForKey:MM_PART_LIST_KEY_STR_]];
                CGFloat otherChargeMoney = [self returnMoneySumFromArrayByKeyStr:MM_OTHER_CHARGE_CHARGE_MONEY_KEY_STR_ withArray:[[arrIndicationTmp objectAtIndex:j] objectForKey:MM_OTHER_CHARGE_LIST_KEY_STR_]];
                fMoneySum += (repairItemMoney+partMoney+otherChargeMoney);
            }
        }
    }
    
    return fMoneySum;
}

-(CGFloat)returnMoneySumFromArrayByKeyStr:(NSString *)keyStr withArray:(NSArray *)array
{
    CGFloat fMoneySum = 0.0;
    
    if ([keyStr isEqualToString:MM_PART_UNITY_PRICE_KEY_STR_])
    {
        for (int i = 0; i<[array count]; i++)
        {
            NSDictionary *dicTmp = [array objectAtIndex:i];
            fMoneySum += ((float)[[dicTmp objectForKey:keyStr] floatValue]) * ((float)[[dicTmp objectForKey:MM_PART_PRECONCERT_QTY_KEY_STR_] floatValue]);
        }
    }
    else
    {
        for (int i = 0; i<[array count]; i++)
        {
            NSDictionary *dicTmp = [array objectAtIndex:i];
            fMoneySum += (float)[[dicTmp objectForKey:keyStr] floatValue];
        }
    }
    
    return fMoneySum;
}

#pragma mark - 重写方法

-(void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType
{
    [super dismissPopupViewControllerWithanimationType:animationType];
    
    //保存数据
    [_editPopViewDis saveData];
    
    //刷新表单
    [self.tableViewDis_1 reloadData];
    [self.tableViewDis_2 reloadData];
}

//设置最后一名客户承担余额
-(void)setUpTheBalanceTagOfLastCustomer
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    NSMutableArray *arrAllocationListTmp = [NSMutableArray arrayWithArray:modelIns.arrAllocationList];
    
    for (int i = 0; i<[arrAllocationListTmp count]; i++)
    {
        NSInteger nCountTmp = [[[arrAllocationListTmp objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] count];
        
        //分配对象
        NSMutableDictionary *dicAllicationTmp = [NSMutableDictionary dictionaryWithDictionary:[arrAllocationListTmp objectAtIndex:i]];
        
        for (int j = 0; j<nCountTmp; j++)
        {
            //dicTmp 可变字典中存储一个客户的信息，更改为承担余额标识
            NSMutableDictionary *dicTmp = [NSMutableDictionary dictionaryWithDictionary:[[[arrAllocationListTmp objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] objectAtIndex:j]];
            
            //设置最后一名客户承担余额
            if (j == nCountTmp-1)
            {
                [dicTmp setObject:[NSString stringWithFormat:@"%d",MM_BALANCE_OWNER] forKey:MM_CUSTOMER_BALANCE_FLAG_KEY_STR_];
            }
            else
            {
                [dicTmp setObject:[NSString stringWithFormat:@"%d",MM_NOT_BALANCE_OWNER] forKey:MM_CUSTOMER_BALANCE_FLAG_KEY_STR_];
            }
            
            /*将该客户加入到数据模型中*/
            
            //取出对应的客户列表，更新数据
            NSMutableArray *arrCustomerListTmp = [NSMutableArray arrayWithArray:[[arrAllocationListTmp objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_]];
            [arrCustomerListTmp setObject:dicTmp atIndexedSubscript:j];
            
            //更新 分配对象 中的数据
            [dicAllicationTmp setObject:arrCustomerListTmp forKey:MM_CUSTOMER_LIST_KEY_STR_];
            
            //更新 客户分配列表 中的数据
            [arrAllocationListTmp setObject:dicAllicationTmp atIndexedSubscript:i];
        }
    }
    
    //更新客户分配列表
    modelIns.arrAllocationList = arrAllocationListTmp;
}

//根据分配状态ID取Name
-(NSString *)getAllocationTypeNameById:(NSString *)allocationTypeId
{
    if ([allocationTypeId isEqualToString:@"0"])
    {
        return @"未分配";
    }
    return @"已分配";
}

//刷新分配列表
-(void)updateAllocationList
{
    /*
     （1）遍历所有的作业项目，
     1、如果有客户的话，设置字段：
     "allocationStatus": "1",－－－已分配
     否则，设置字段：
     "allocationStatus": "1",－－－未分配；
     2、 "allocationType": "0",－－－按金额分配
     */
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    for (int i = 0; i<(NSInteger)[modelIns.arrAllocationList count]; i++)
    {
        NSMutableDictionary *dicAllocationListTmp = [NSMutableDictionary dictionaryWithDictionary:[modelIns.arrAllocationList objectAtIndex:i]];
        
        if (0 == [[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] count])
        {
            [dicAllocationListTmp setObject:[NSString stringWithFormat:@"%i",MM_ALLOCATED_] forKey:MM_ALLOCATION_ALLOCATION_STATUS_KEY_STR_];
        }
        else
        {
            [dicAllocationListTmp setObject:[NSString stringWithFormat:@"%i",MM_NOT_ALLOCATED] forKey:MM_ALLOCATION_ALLOCATION_STATUS_KEY_STR_];
        }
        
        [dicAllocationListTmp setObject:[NSString stringWithFormat:@"%i",MM_ALLOCATE_BY_MONEY] forKey:MM_ALLOCATION_ALLOCATION_TYPE_KEY_STR_];
    }
    
    //初始化Label
//    NSLog(@"%i",_nRowAllocationListToSel);
//    NSLog(@"%i",_nRowCustomerListToSel);
//    NSLog(@"%@",modelIns.arrAllocationList);
//    
//    NSLog(@"%@",modelIns.dicJobSheetMsg);
    
    NSDictionary *dicTmp = [modelIns.arrAllocationList objectAtIndex:_nRowAllocationListToSel];
    
    for (int i = 0; i<(NSInteger)[_arrAllKeysOfAllocationList count]; i++)
    {
        NSString *strKey = [_arrAllKeysOfAllocationList objectAtIndex:i];
        
        //服务类型
        if ([strKey isEqualToString:MM_ALLOCATION_SERVICE_TYPE_KEY_STR_])
        {
            ((UILabel *)[_dicAllLblsWhileAllocationListToBeSelected objectForKey:strKey]).text = [HelpClass getServiceTypeNameById:[dicTmp objectForKey:MM_ALLOCATION_SERVICE_TYPE_KEY_STR_]];
            continue;
        }
        
        //分配状态
        if ([strKey isEqualToString:MM_ALLOCATION_ALLOCATION_STATUS_KEY_STR_])
        {
            
            ((UILabel *)[_dicAllLblsWhileAllocationListToBeSelected objectForKey:strKey]).text = [self getAllocationTypeNameById:[dicTmp objectForKey:strKey]];
            continue;
        }
        
        ((UILabel *)[_dicAllLblsWhileAllocationListToBeSelected objectForKey:strKey]).text = [dicTmp objectForKey:strKey];
    }
}

//刷新客户列表
-(void)updateCustomerList
{
    /*
     （1）默认最后一名客户承担余额，
     最后一名客户设置：
     "money": "0",－－－totalMoney-otherMoney
     "proportion": "100.00",－－－money/totalMoney
     "balanceFlag": "0"－－－承担余额
     
     （2）其他用户设置
     "money": "0",－－－totalMoney-otherMoney
     "proportion": "100.00",－－－money/totalMoney
     "balanceFlag": "0"－－－不承担余额
     */
    
    /*
     （1）默认最后一名客户承担余额，
     最后一名客户设置：
     "money": "0",－－－totalMoney-otherMoney
     "proportion": "100.00",－－－money/totalMoney
     "balanceFlag": "0"－－－承担余额
     */
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    for (int i = 0; i<[modelIns.arrAllocationList count]; i++)
    {
        NSInteger nCustomerCountTmp = [[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] count];
        if (nCustomerCountTmp<=0)
        {
            continue;
        }
        NSMutableDictionary *dicTheLastCustomerTmp = [NSMutableDictionary dictionaryWithDictionary:[[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] objectAtIndex:nCustomerCountTmp-1]];
        
        NSString *strMoney = [NSString stringWithFormat:@"%0.2f",([self totalMoneyByAllocationList:i]-[self otherTotalMoneyByAllocationList:i andCustomerList:nCustomerCountTmp-1])];
        if ([strMoney floatValue] <= 0.00)
        {
            strMoney = @"0.00";
        }
        //设置金额为余额
        [dicTheLastCustomerTmp setObject:strMoney forKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_];
        //分配比例
        [dicTheLastCustomerTmp setObject:[NSString stringWithFormat:@"%i",(NSInteger)([[dicTheLastCustomerTmp objectForKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_] floatValue]*100.0/[self totalMoneyByAllocationList:i])] forKey:MM_CUSTOMER_PROPORTION_KEY_STR_];
        //余额担保
        [dicTheLastCustomerTmp setObject:[NSString stringWithFormat:@"%i",MM_BALANCE_OWNER] forKey:MM_CUSTOMER_BALANCE_FLAG_KEY_STR_];
        
        //保存数据
        NSMutableArray *arrCustomerListTmp = [NSMutableArray arrayWithArray:[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_]];
        [arrCustomerListTmp setObject:dicTheLastCustomerTmp atIndexedSubscript:nCustomerCountTmp-1];
        
        NSMutableDictionary *dicAllocationTmp = [NSMutableDictionary dictionaryWithDictionary:[modelIns.arrAllocationList objectAtIndex:i]];
        [dicAllocationTmp setObject:arrCustomerListTmp forKey:MM_CUSTOMER_LIST_KEY_STR_];
        
        modelIns.arrAllocationList = [NSMutableArray arrayWithArray:modelIns.arrAllocationList];
        [modelIns.arrAllocationList setObject:dicAllocationTmp atIndexedSubscript:i];
    }
    
    /*
     （2）其他用户设置
     "proportion": "100.00",－－－money/totalMoney
     "balanceFlag": "0"－－－不承担余额
     */
    for (int i = 0; i<[modelIns.arrAllocationList count]; i++)
    {
        NSMutableArray *arrCustomerListTmp = [NSMutableArray arrayWithArray:[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_]];
        
        for (int j = 0; j<(NSInteger)([arrCustomerListTmp count]-1); j++)
        {
//            NSMutableDictionary *dicTheLastCustomerTmp = [NSMutableDictionary dictionaryWithDictionary:[[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] objectAtIndex:nCustomerCountTmp-1]];
            
            NSMutableDictionary *dicTheLastCustomerTmp = [NSMutableDictionary dictionaryWithDictionary:[arrCustomerListTmp objectAtIndex:j]];
            
            //分配比例
            [dicTheLastCustomerTmp setObject:[NSString stringWithFormat:@"%0.2f",([[dicTheLastCustomerTmp objectForKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_] floatValue]/[self totalMoneyByAllocationList:i])] forKey:MM_CUSTOMER_PROPORTION_KEY_STR_];
            //余额担保
            [dicTheLastCustomerTmp setObject:[NSString stringWithFormat:@"%i",MM_NOT_BALANCE_OWNER] forKey:MM_CUSTOMER_BALANCE_FLAG_KEY_STR_];
            
            //保存数据
            NSMutableArray *arrCustomerListTmp = [NSMutableArray arrayWithArray:[[modelIns.arrAllocationList objectAtIndex:i] objectForKey:MM_CUSTOMER_LIST_KEY_STR_]];
            [arrCustomerListTmp setObject:dicTheLastCustomerTmp atIndexedSubscript:j];
            
            NSMutableDictionary *dicAllocationTmp = [NSMutableDictionary dictionaryWithDictionary:[modelIns.arrAllocationList objectAtIndex:i]];
            [dicAllocationTmp setObject:arrCustomerListTmp forKey:MM_CUSTOMER_LIST_KEY_STR_];
            
            modelIns.arrAllocationList = [NSMutableArray arrayWithArray:modelIns.arrAllocationList];
            [modelIns.arrAllocationList setObject:dicAllocationTmp atIndexedSubscript:i];
        }
    }
}

//指定分配列表行的总金额
-(CGFloat)totalMoneyByAllocationList:(NSInteger)allocationRowSelXingCan
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    return [[[modelIns.arrAllocationList objectAtIndex:allocationRowSelXingCan] objectForKey:MM_ALLOCATION_REPAIR_MONEY_KEY_STR_] floatValue];
}

//指定分配列表行中的除过指定客户列表行的其它总金额
-(CGFloat)otherTotalMoneyByAllocationList:(NSInteger)allocationRowSelXingCan andCustomerList:(NSInteger)customerListRowSelXingCan
{
    CGFloat fMoneySum = 0.00;
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrCustomerListTmp = [[modelIns.arrAllocationList objectAtIndex:allocationRowSelXingCan] objectForKey:MM_CUSTOMER_LIST_KEY_STR_];
    
    for (int i = 0; i<[arrCustomerListTmp count]; i++)
    {
        if (i == customerListRowSelXingCan)
        {
            continue;
        }
        
        fMoneySum += [[[arrCustomerListTmp objectAtIndex:i] objectForKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_] floatValue];
    }
    
    return fMoneySum;
}

- (IBAction)editCustomerBtnPressed:(UIButton *)sender
{
    _editPopViewDis = nil;
    _editPopViewDis = [[mm_multipleCustomerToAllocateEditViewController alloc] initWithNibName:@"mm_multipleCustomerToAllocateEditViewController" bundle:nil];
    
    //初始化 弹出界面的控件内容
//    _editPopViewDis.customerIdLbl.text = ((UILabel *)[cell.dicForLblsOnCell objectForKey:MM_CUSTOMER_NUMBER_KEY_STR_]).text;
//    _editPopViewDis.customerNameLbl.text = ((UILabel *)[cell.dicForLblsOnCell objectForKey:MM_CUSTOMER_NAME_KEY_STR_]).text;
//    _editPopViewDis.repairMoneyTxt.text = ((UITextField *)[cell.dicForLblsOnCell objectForKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_]).text;
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if ([[[modelIns.arrAllocationList objectAtIndex:_nRowAllocationListToSel] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] count] == 0)
    {
        //提示没有客户存在
        UIAlertView *alerVeiw = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"客户不存在！" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [alerVeiw show];
        
        return;
    }
    NSDictionary *dicCustomerTmp = [[[modelIns.arrAllocationList objectAtIndex:_nRowAllocationListToSel] objectForKey:MM_CUSTOMER_LIST_KEY_STR_] objectAtIndex:_nRowCustomerListToSel];
    
    _editPopViewDis.customerIdLbl.text = [dicCustomerTmp objectForKey:MM_CUSTOMER_NUMBER_KEY_STR_];
    _editPopViewDis.customerNameLbl.text = [dicCustomerTmp objectForKey:MM_CUSTOMER_NAME_KEY_STR_];
    _editPopViewDis.repairMoneyTxt.text = [dicCustomerTmp objectForKey:MM_CUSTOMER_REPAIR_MONEY_KEY_STR_];
    
    _editPopViewDis.nRowAllocationListToSel = _nRowAllocationListToSel;
    _editPopViewDis.nRowCustomerListToSel = _nRowCustomerListToSel;
    
    [self presentPopupViewController:_editPopViewDis animationType:MJPopupViewAnimationFade];
}

@end
